package org.gensokyo.shanbay.dao

import java.time.LocalDate
import java.util.concurrent.TimeUnit

import com.typesafe.scalalogging.slf4j.StrictLogging

import scala.concurrent.Await
import scala.concurrent.duration.Duration


/**
 * Created by liusen on 15-12-7.
 */
object UserInfoDao extends MySqlTrait with StrictLogging {

  import slick.driver.MySQLDriver.api._

  val userInfo = TableQuery[UserInfo]

  private def getUsers: List[(String, String, Int, LocalDate)] = Await.result(db.run(
    userInfo.map(x => (x.userName, x.password, x.days, x.lastClockDate)).result
  ), Duration(200000L, TimeUnit.MILLISECONDS)).toList

  def updateDaysAndDate(userName: String, lastClockDate: String = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDate.now())): Unit =
    Await.result(db.run(
      sqlu"""update USER_INFO set days = days +1 , last_clock_date = ${lastClockDate} where user_name = ${userName}"""
    ), Duration(200000L, TimeUnit.MILLISECONDS))

  def groupedUserList = {
    val ll = getUsers.groupBy(_._3 > 6)
    (ll.apply(true), ll.apply(false))
  }

  def getAllUsers: List[(String, String)] = Await.result(db.run(
    userInfo.map(x => (x.userName, x.password)).result
  ), Duration(200000L, TimeUnit.MILLISECONDS)).toList

}
